﻿/********************************************
 *Author:       leev
 *CreateDate:   2015-5-4
 *Description:  数据缓存操作类 DataCacheHelper
 *******************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace YiYiTong.Rocket.Helper
{
    public class DataCacheHelper
    {
        #region 数据缓存操作类
        static System.Web.Caching.Cache objCache = null;
        static DataCacheHelper()
        {
            objCache = HttpRuntime.Cache;
        }
        /// <summary>
        /// 通过缓存键值获取缓存中的对象
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="CacheKey">键</param>
        /// <returns></returns>
        public static T Get<T>(string CacheKey)
        {
            T obj = default(T);
            try
            {
                obj = (T)Convert.ChangeType(objCache[CacheKey], typeof(T));
            }
            catch
            {
            }
            return obj;
        }
        /// <summary>
        /// 简单的设置缓存
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="CacheKey">键</param>
        /// <param name="obj">泛型对象</param>
        public static void Set<T>(string CacheKey, T obj)
        {
            objCache.Insert(CacheKey, obj);
        }
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值,可设置固定过时时间(秒)
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="CacheKey">键</param>
        /// <param name="obj">泛型对象</param>
        /// <param name="times">过期时间</param>
        public static void Set<T>(string CacheKey, T obj, DateTime time)
        {
            objCache.Insert(CacheKey, obj, null, time, Cache.NoSlidingExpiration);
        }
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值,可设置空闲过时时间(秒)
        /// </summary>
        /// <param name="CacheKey">键</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="times">缓存空闲时间(秒)[eg:如果缓存在times秒内无读取该缓存操作，则缓存自动失效]</param>
        public static void Set<T>(string CacheKey, T obj, int times)
        {
            Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, obj, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(times));
        }
        /// <summary>
        /// 通过键移除指定缓存
        /// </summary>
        /// <param name="CacheKey">键</param>
        public static void Remove(string CacheKey)
        {
            Cache objCache = HttpRuntime.Cache;
            if (!CacheKey.Equals(""))
                objCache.Remove(CacheKey);
        }
        /// <summary>
        /// 移除所有缓存对象
        /// </summary>
        public static void RemoveAll()
        {
            Cache objCache = HttpRuntime.Cache;
            IDictionaryEnumerator CacheEnum = objCache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                string skey = CacheEnum.Key.ToString().Trim();
                objCache.Remove(skey);
            }
        }

        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值,抽象工厂用到
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string CacheKey)
        {
            return objCache[CacheKey];
        }
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值，抽象工厂用到
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject)
        {
            objCache.Insert(CacheKey, objObject);
        }
        #endregion
    }
}
